package com;

import cn.hutool.core.lang.Console;
import cn.hutool.cron.CronUtil;
import cn.hutool.cron.task.Task;
import com.bean.UserTicket;
import com.bean.WeiXinUser;
import com.gzcssutils.AccessTokenThread;
import com.gzcssutils.AccessUserInfo;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Logger;

public class LoginCtrlServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger("com.LoginCtrlServlet");

    /**
     * Default constructor.
     */
    public LoginCtrlServlet() {

    }

    static {
        //每小时执行一次
        CronUtil.schedule("0 0 0/1 * * ? ", new Task() {
            @Override
            public void execute() {
                Console.log("com.utils.AccessToken Task excuted AT:" + new Date());
                AccessTokenThread.getAccessToken();
            }
        });

        // 支持秒级别定时任务
        CronUtil.setMatchSecond(true);
        CronUtil.start();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  {
        String code = request.getParameter("code");
        //获取user_ticket
        UserTicket userTicket = AccessUserInfo.getUserTicket(code);
        if(null != userTicket) {
            String userId = userTicket.getUserid();
            //获取成员信息
            WeiXinUser user = AccessUserInfo.getUser(userId);
        }

        RequestDispatcher rq = request.getRequestDispatcher("/index.jsp");
        try {
            rq.forward(request,response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
}
